"""
产品管理API路由
"""

from fastapi import APIRouter, HTTPException
from typing import List, Dict, Any

from models.product_models import ProductCreateRequest, ProductUpdateRequest
from managers.stripe_manager import StripeManager

router = APIRouter(prefix="/api/products", tags=["产品管理"])

# 创建全局的Stripe管理器实例
stripe_manager = StripeManager()


@router.post("/", response_model=Dict[str, Any])
async def create_product(request: ProductCreateRequest):
    """创建新产品"""
    return stripe_manager.product_service.create_product(request)


@router.get("/", response_model=List[Dict[str, Any]])
async def list_products(limit: int = 100, offset: int = 0):
    """列出所有产品"""
    return stripe_manager.product_service.list_products(limit, offset)


@router.get("/{product_id}", response_model=Dict[str, Any])
async def get_product(product_id: str):
    """获取产品信息"""
    product = stripe_manager.product_service.get_product(product_id)
    if not product:
        raise HTTPException(status_code=404, detail="产品不存在")
    return product


@router.put("/{product_id}", response_model=Dict[str, Any])
async def update_product(product_id: str, request: ProductUpdateRequest):
    """更新产品信息"""
    product = stripe_manager.product_service.update_product(product_id, request)
    if not product:
        raise HTTPException(status_code=404, detail="产品不存在")
    return product


@router.delete("/{product_id}")
async def delete_product(product_id: str):
    """删除产品"""
    success = stripe_manager.product_service.delete_product(product_id)
    if not success:
        raise HTTPException(status_code=404, detail="产品不存在")
    return {"message": "产品删除成功"}
